package mc.support.zmq;

import mc.support.proto.*;
import mc.support.zmq.err.ZmqTimeoutException;
import mc.support.zmq.route.ZmqConnection;
import org.zeromq.ZMQ;

/**
 * Created with IntelliJ IDEA. <br/>
 * Author: Francis Yuen    <br/>
 * Date: 2014-10-29  <br/>
 */
public final class ProtoHelper implements ProtoConstants {

    private ProtoHelper() {
    }

    public static GeneralReq toGeneralReq( TitleGenReq req ) {
        return GeneralReq.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_GEN )
                .setTitleGenReq( req )
                .build();
    }

    public static GeneralReq toGeneralReq( TitleCompareReq req ) {
        return GeneralReq.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_COMPARE )
                .setTitleCompareReq( req )
                .build();
    }

    public static GeneralReq toGeneralReq( TitleAutoOptimReq req ) {
        return GeneralReq.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_AUTO_OPTIM )
                .setTitleAutoOptimReq( req )
                .build();
    }

    public static GeneralReq toGeneralReq( ShopAnalyseReq req ) {
        return GeneralReq.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.SHOP_ANALYSE )
                .setShopAnalyseReq( req )
                .build();
    }

    public static GeneralReq toGeneralReq( TitleBrandReq req ) {
        return GeneralReq.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_BRAND )
                .setTitleBrandReq( req )
                .build();
    }

    public static GeneralRsp toGeneralRsp( TitleCompareRsp rsp ) {
        return GeneralRsp.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_COMPARE )
                .setTitleCompareRsp( rsp )
                .build();
    }

    public static GeneralRsp toGeneralRsp( TitleGenRsp rsp ) {
        return GeneralRsp.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_GEN )
                .setTitleGenRsp( rsp )
                .build();
    }

    public static GeneralRsp toGeneralRsp( TitleAutoOptimRsp rsp ) {
        return GeneralRsp.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_AUTO_OPTIM )
                .setTitleAutoOptimRsp( rsp )
                .build();
    }

    public static GeneralRsp toGeneralRsp( ShopAnalyseRsp rsp ) {
        return GeneralRsp.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.SHOP_ANALYSE )
                .setShopAnalyseRsp( rsp )
                .build();
    }

    public static GeneralRsp toGeneralRsp( TitleBrandRsp rsp ) {
        return GeneralRsp.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.TITLE_BRAND )
                .setTitleBrandRsp( rsp )
                .build();
    }

    public static GeneralRsp sysPingRsp() {
        return GeneralRsp.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.SYS_PING )
                .build();
    }

    public static GeneralReq sysPingReq() {
        return GeneralReq.newBuilder()
                .setMagicCode( MAGIC_CODE )
                .setBiz( Biz.SYS_PING )
                .build();
    }

    public static boolean sysPing( ZmqConnection conn ) {
        boolean success = false;
        try {
            GeneralRsp rsp = GeneralRsp.parseFrom( conn.sendAndWaitRsp( sysPingReq().toByteArray() ) );
            success = ( rsp.getBiz() == Biz.SYS_PING );
        } catch ( Exception err ) {
            err.printStackTrace();
        }
        return success;
    }

    public static byte[] sendAndWaitRsp( final ZMQ.Socket socket, final byte[] bs, final long timeout ) {
        socket.send( bs, 0 );
        if ( ZMQ.poll( new ZMQ.PollItem[] { new ZMQ.PollItem( socket, ZMQ.Poller.POLLIN ) }, timeout ) > 0 ) {
            return socket.recv( 0 );
        } else {
            throw new ZmqTimeoutException( "no reply from remote server after " + timeout + " ms" );
        }
    }

}
